package examples

import (
	"fmt"
	"gitee.com/Cauchy_AQ/gokv"
	gokvRedis "gitee.com/Cauchy_AQ/gokv/redis"
	"gitee.com/Cauchy_AQ/gokv/utils"
)

func ExamplesRedis() {
	opts := gokv.DefaultOptions

	redis, err := gokvRedis.NewRedisDataStruct(&opts)
	defer redis.Close()
	if err != nil {
		panic(err)
	}

	// string
	err = redis.Set([]byte("string1"), []byte("string1-value"), 0)
	if err != nil {
		panic(err)
	}
	val, err := redis.Get([]byte("string1"))
	if err != nil {
		panic(err)
	}
	fmt.Println(string(val))

	// hash
	ok, err := redis.HSet([]byte("hash1"), []byte("key1"), []byte("value1"))
	if err != nil {
		panic(err)
	}
	fmt.Println(ok)
	val, err = redis.HGet([]byte("hash1"), []byte("key1"))
	if err != nil {
		panic(err)
	}
	fmt.Println(string(val))
	exist, err := redis.HExists([]byte("hash1"), []byte("key1"))
	if err != nil {
		panic(err)
	}
	fmt.Println(exist)
	size := redis.HLen([]byte("hash1"))
	fmt.Println(size)
	keys, err := redis.HKeys([]byte("hash1"))
	if err != nil {
		panic(err)
	}
	fmt.Println(utils.ByteArrayToStringArray(keys))
	vals, err := redis.HVals([]byte("hash1"))
	if err != nil {
		panic(err)
	}
	fmt.Println(utils.ByteArrayToStringArray(vals))
	ok, err = redis.HDel([]byte("hash1"), []byte("key1"))
	if err != nil {
		panic(err)
	}
	fmt.Println(ok)

	// set
	count, _ := redis.SAdd([]byte("set1"), []byte("set-value1"), []byte("set-value2"), []byte("set-value3"))
	fmt.Println(count)
	ok = redis.SIsMember([]byte("set1"), []byte("set-value1"))
	fmt.Println(ok)
	count, _ = redis.SRem([]byte("set1"), []byte("set-value1"), []byte("set-value2"))
	fmt.Println(count)
	size, _ = redis.SCard([]byte("set1"))
	fmt.Println(size)
	members, _ := redis.SMembers([]byte("set1"))
	fmt.Println(utils.ByteArrayToStringArray(members))

	// list
	lens, _ := redis.LPush([]byte("list1"), []byte("list1-value1"))
	fmt.Println(lens)
	lens, _ = redis.RPush([]byte("list1"), []byte("list1-value2"))
	fmt.Println(lens)
	val, _ = redis.LIndex([]byte("list1"), -1)
	fmt.Println(string(val))
	lens, _ = redis.LLen([]byte("list1"))
	fmt.Println(lens)
	val, _ = redis.RPop([]byte("list1"))
	fmt.Println(string(val))
	val, _ = redis.LPop([]byte("list1"))
	fmt.Println(string(val))
}
